package com.ojoin.trade.bpm.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ojoin.trade.bpm.web.service.BpmModelService;
import com.ojoin.trade.bpm.web.support.Page;
import com.ojoin.trade.common.utils.json.JSONUtils;
import com.ojoin.trade.common.web.controller.SimpleBaseController;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.domain.Result.Status;

/**
 * 流程模型相关Controller
 */
@Controller
@RequestMapping(value = "/bpm/model")
public class BpmModelController extends SimpleBaseController {

	@Autowired
	private BpmModelService bpmModelService;

	/**
	 * 流程模型列表
	 */
	@RequiresPermissions("bpm:model:edit")
	@ResponseBody
	@RequestMapping("list")
	public Result modelList(String category, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<org.activiti.engine.repository.Model> page = bpmModelService.modelList(
				new Page<org.activiti.engine.repository.Model>(), category);
		return new Result(Status.OK,page.convert());
	}

	/**
	 * 创建模型
	 * @throws IOException 
	 */
	@RequiresPermissions("bpm:model:edit")
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public void create(String name, String key, String description, String category,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
			org.activiti.engine.repository.Model modelData = bpmModelService.create(name, key, description, category);
		  String editorUrl=request.getContextPath() + "/bpm/rest/service/editor?id=" + modelData.getId();
		  response.getWriter().write(editorUrl);
	}

	/**
	 * 根据Model部署流程
	 */
	@RequiresPermissions("bpm:model:edit")
	@ResponseBody
	@RequestMapping(value = "deploy")
	public Result deploy(String id) {
		String message = bpmModelService.deploy(id);
		return new Result(Result.Status.OK, "部署流程完成");
	}
	
	/**
	 * 导出model的xml文件
	 */
	@RequiresPermissions("bpm:model:edit")
	@RequestMapping(value = "export")
	public void export(String id, HttpServletResponse response) {
		bpmModelService.export(id, response);
	}

	/**
	 * 更新Model分类
	 */
	@RequiresPermissions("bpm:model:edit")
	@ResponseBody
	@RequestMapping(value = "updateCategory")
	public Result updateCategory(String id, String category) {
		bpmModelService.updateCategory(id, category);
		return new Result(Result.Status.OK, "修改成功");
	}
	
	/**
	 * 删除Model
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("bpm:model:edit")
	@ResponseBody
	@RequestMapping(value = "delete")
	public Result delete(String id) {
		bpmModelService.delete(id);
		return new Result(Result.Status.OK, "删除成功");
	}
}
